import { loginAPI } from "@/api/user"
import { setToken, getToken, removeToken } from '@/utils/auth'
export default {
    namespaced: true,
    state: {
        token: getToken() || ''
    },
    mutations: {
        //设置vuex中的token
        setToken(state, token) {
            //设置vuex中的token
            state.token = token
                //把token写到浏览器的token中
            setToken(token)
        },
        removeToken(state) {
            state.token = ''
            removeToken()
        }
    },
    actions: {
        async loginAction(context, data) {
            //async await:对内同步 对外异步
            const res = await loginAPI(data)
            context.commit('setToken', res.data.token)
            console.log(res.data.token)
        }
    }
}